package com.business.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.UserBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.ConstantCode;
import net.sf.rose.util.DateFormat;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.business.system.bean.WxUserBean;
import com.business.system.util.DBTools;
import com.business.system.util.PageUtil;
import com.business.system.util.ResponseData;

/**
 * @author: xudy
 * @date: 2018/04/10 15:08
 * @description:
 */
@RestController
@RequestMapping("user")
public class UserAction extends BaseAction {

	@RequestMapping("/login.do")
	public ResponseData loginUserAndPassword(Service service, HttpServletRequest request, @RequestParam String username, @RequestParam String password) {
		WxUserBean bean = DBTools.getBean(service, WxUserBean.class, "select * from WX_USER where LOGIN_NAME=? AND PASSWORD=?", username, password);
		if (bean != null) {
			// 设置token
			bean.setToken(KeyGenerator.getSystemUUID());
			DBUtils.update(service, bean);
			request.getSession().setAttribute(WxUserBean.LOGIN_USER, bean);
            UserBean user = new UserBean();
            user.setUserName(bean.getLoginName());
            user.setUserID(bean.getLoginName());
            user.setLastLoginTime(DateFormat.getTimestamp());
            request.getSession().setAttribute(ConstantCode.USER_BEAN_NAME,user);
			return ResponseData.buildResponseData(200, bean);
		}
		return null;
	}

	@RequestMapping("/logout.do")
	public ResponseData logOut(Service service, HttpServletRequest request) {
		request.getSession().removeAttribute("loginuser");
		return ResponseData.buildResponseData(200, null);
	}

	@RequestMapping("/getUserInfo.do")
	public ResponseData getUserInfo(Service service, String token) {
		WxUserBean bean = DBTools.getBean(service, WxUserBean.class, "select * from WX_USER where TOKEN=?", token);
		return ResponseData.buildResponseData(200, bean);
	}

	@RequestMapping("getUserList.do")
	public PageBean getUserList(Service service, HttpServletRequest request) {
		PageBean page = PageUtil.page(request);
		StringBuilder sql = new StringBuilder();
		sql.append("select * from wx_user where 1=1");
		Map<String, Object> params = WebUtils.getRequestData(request);
		List<Object> condition = new ArrayList<>();
		if (params.containsKey("content")) {
			sql.append(" and (NICKNAME like '%" + params.get("content") + "%' or MOBILE like '%"+params.get("content")+"%' or USER_NAME like '%"+params.get("content")+"%')");
		}
		DBTools.getDataList(service, sql.toString(), page);
		List<Object> data = page.getData();
		if (data.size() > 0) {
            List<Map<String, Object>> agentList = null;
            List<Map<String, Object>> jiamengList = null;
            try {
                // 获取代理
                agentList = brandService.getApplyBrand(service, 1);
                // 获取加盟
                jiamengList = brandService.getApplyBrand(service, 2);
                for (Object d : data) {
                    Map<String, Object> d1 = (Map<String, Object>) d;
                    for (Map<String, Object> a1 : agentList) {
                        if (d1.get("ID").equals(a1.get("USER_ID"))) {
                            d1.put("agent_" + a1.get("BRAND_ID").toString(), "√");
                        }
                    }
                    for (Map<String, Object> a1 : jiamengList) {
                        if (d1.get("ID").equals(a1.get("USER_ID"))) {
                            d1.put("jiameng_" + a1.get("BRAND_ID").toString(), "√");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
		}
		return page;
	}
}